﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using WinFormUIXMLParser.ChildForms;


namespace WinFormUIXMLParser
{
    public partial class Form1 : Form
    {
      
        private string _saveFolder = "";
        private string _sourceFile = "";
        private string _sourceFolder = "";
        private bool _isMultiMode = false;

        public Form1()
        {
            InitializeComponent();

            int a = 5;
            int b = a++;
            b = ++a;

        }

        private void buttonCancell_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOpenSource_Click(object sender, EventArgs e)
        {
            textBoxSource.Text = "";
            if(_isMultiMode)
            {
                FolderBrowserDialog form = new FolderBrowserDialog();
                form.SelectedPath = @"E://";
                if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    _sourceFolder = form.SelectedPath;
                    textBoxSource.Text = _sourceFolder;
                }
            }
            else
            {
                OpenFileDialog form = new OpenFileDialog();
                form.InitialDirectory = @"E://Source";
                form.Filter = "设计器代码文件(*.cs)|*.cs";
                if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    _sourceFile = form.FileName;
                    textBoxSource.Text = _sourceFile;
                }
            }
        }

        private void buttonSaveOut_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog form = new FolderBrowserDialog();
            form.SelectedPath = @"D://out";
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _saveFolder = form.SelectedPath;
                textBoxSaveOut.Text = _saveFolder;
            }
        }

        //public virtual override void OnReading()
        //{
        //    string xmlFile = @"D:\outting\PageFillFirstInfo.xml";
        //    XmlReadConfig configReader = new XmlReadConfig(xmlFile);
        //    configReader.OnReading();
        //    XmlWriteConfig configWriter = new XmlWriteConfig(configReader);
        //    configWriter.Write(@"D://outting//temp.xml");
        //}

        private void OnHandling(string src, string dest)
        {
            CodeParser _parser = new CodeParser(src);
            _parser.OnHandling();
            XmlWriteConfig config = new XmlWriteConfig(_parser, dest);
            config.Write();
        }

        private void OnHanding()
        {
            _saveFolder = textBoxSaveOut.Text;
            if (_isMultiMode)
            {
                _sourceFolder = textBoxSource.Text;
                var files = Directory.GetFiles(_sourceFolder, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".Designer.cs"));
                int total = files.Count();
                progressBar1.Step = 0;
                progressBar1.Maximum = total;
                int i = 0;
                foreach(string src in files)
                {
                    string fileName = Path.GetFileNameWithoutExtension(src);
                    string[] words = fileName.Split('.');
                    string xmlFile = string.Format("{0}/{1}.xml", _saveFolder, words[0]);
                    OnHandling(src, xmlFile);
                    i++;
                    progressBar1.Step = i;
                }
                System.Diagnostics.Process.Start("explorer.exe", _saveFolder);
            }
            else
            {
                string xmlFile = "";
                _sourceFile = textBoxSource.Text;
                if (File.Exists(_sourceFile) || Directory.Exists(_saveFolder))
                {
                    string fileName = Path.GetFileNameWithoutExtension(_sourceFile);
                    string[] words = fileName.Split('.');
                    xmlFile = string.Format("{0}/{1}.xml", _saveFolder, words[0]);
                }
                OnHandling(_sourceFile, xmlFile);
                System.Diagnostics.Process.Start("explorer.exe", _saveFolder);
            }
        }

        private void buttonDoMult_Click(object sender, EventArgs e)
        {
            OnHanding();
        }

        private void buttonTestClick(object sender, EventArgs e)
        {
            MessageBox.Show("测试成功啦!");
        }

        private void radioButtonSingle_CheckedChanged(object sender, EventArgs e)
        {
            _isMultiMode = !radioButtonSingle.Checked;
            buttonSaveOut.Text =_isMultiMode ? "打开目录" : "打开文件";
        }

        private void radioButtonMulti_CheckedChanged(object sender, EventArgs e)
        {
            _isMultiMode = radioButtonMulti.Checked;
            buttonSaveOut.Text = _isMultiMode ? "打开目录" : "打开文件";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Type t = this.GetType();
            //System.Reflection.MethodInfo methodInfo = t.GetMethod("buttonTestClick", BindingFlags.Instance | BindingFlags.NonPublic);
            //Delegate DelegateInstance = StaticFuns.CreateDelegateFromMethodInfoByDelegate<EventHandler>(this, methodInfo);
            //EventHandler handler = DelegateInstance as EventHandler;
            //buttonTest.Click += handler;
            System.Reflection.MethodInfo methodInfo1 = t.GetMethod("buttonTest_KeyDown111", BindingFlags.Instance | BindingFlags.NonPublic);
            EventHandler<KeyEventArgs> DelegateInstance1 = StaticTypes.CreateDelegateFromMethodInfo<KeyEventArgs>(this, methodInfo1);
            KeyEventHandler handler = new KeyEventHandler(DelegateInstance1);
            buttonTest.KeyDown += handler;
        }

        private void buttonTest_KeyDown111(object sender, KeyEventArgs e)
        {

        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            string src = @"E:\01-WORK\2-流口\Git_V2\WH_GA_Form_Self_V2\Code\WinFormUIXMLParser\WinFormUIXMLParser\WinFormUIXMLParser\Form2.Designer.cs";
            string dest = @"E:\SaveConfig\UI.xml";
            CodeParser _parser = new CodeParser(src);
            _parser.OnHandling();
            WriteUIConfig config = new WriteUIConfig(_parser, dest);
            config.Write();
            MessageBox.Show("解析成功!");
            //StringBuilder builder = new StringBuilder();
            //builder.Append("本次回合的测量结果是:");
            //builder.Append(1);
            //builder.Append(Environment.NewLine);
            //MessageBox.Show(builder.ToString());
            //string ts = "private System.Windows.Forms.TextBox  ";
            //MessageBox.Show(ts.Trim());           
            // string strReadFilePath = @"D://out//test.cs";

           // CodeParser parser = new CodeParser(@"D://out//test.cs");
           //  parser.OnHandling();
           // MessageBox.Show(parser.ItemsCount.ToString());
           // FormManager form = new FormManager();
           // form.ShowDialog();

        }

        private void textBoxSource_Enter(object sender, EventArgs e)
        {

        }
    }
}
